/**
 * My simple header template from child directory
 * ConcurrencyClass.java - ${projectName} - ${organizationName} - ${inceptionYear}
 */
import org.apache.log4j.Logger;

import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 09.03.14
 * Time: 16:26
 * To change this template use File | Settings | File Templates.
 */
public class ConcurrencyClass {

    private static final int time = 100;
    private static Logger logger = Logger.getLogger(ConcurrencyClass.class);

    public synchronized void m1() {
        logger.info("Thread " + currentThread().getName() + " entered m1 method");
        try {
            sleep(time);
        } catch (InterruptedException ignored) {}
        logger.info("Thread " + currentThread().getName() + " leaved m1 method");
    }

    public synchronized void m2() {
        logger.info("Thread " + currentThread().getName() + " entered m2 method");
        try {
            sleep(time);
        } catch (InterruptedException ignored) {}
        logger.info("Thread " + currentThread().getName() + " leaved m2 method");
    }

    public void m3() {
        logger.info("Thread " + currentThread().getName() + " entered m3 method");
        try {
            Thread.sleep(time/2);
        } catch (InterruptedException ignored) {}
        synchronized (this) {
            logger.info("Thread " + currentThread().getName() + " entered m3 block");
            try {
                sleep(time);
            } catch (InterruptedException ignored) {}
            logger.info("Thread " + currentThread().getName() + " leaved m3 block");
        }
        logger.info("Thread " + currentThread().getName() + " leaved m3 method");
    }

    public void m4() {
        logger.info("Thread " + currentThread().getName() + " entered m4 method");
        try {
            Thread.sleep(time/2);
        } catch (InterruptedException ignored) {}
        synchronized (this) {
            logger.info("Thread " + currentThread().getName() + " entered m4 block");
            try {
                sleep(time);
            } catch (InterruptedException ignored) {}
            logger.info("Thread " + currentThread().getName() + " leaved m4 block");
        }
        logger.info("Thread " + currentThread().getName() + " leaved m4 method");
    }

    public static synchronized void m5() {
        logger.info("Thread " + currentThread().getName() + " entered m5 method");
        try {
            sleep(time);
        } catch (InterruptedException ignored) {}
        logger.info("Thread " + currentThread().getName() + " leaved m5 method");
    }

}
